package com.cmcc.groupcontacts.common;

import android.content.Context;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.Message;

import com.cmcc.groupcontacts.MainApp;
import com.cmcc.groupcontacts.MainApp.AfterBlockedCallbackEvent;
import com.cmcc.groupcontacts.firewall.business.CallListBusiness;
import com.cmcc.groupcontacts.util.ExceptionHandler;
import com.cmcc.groupcontacts.util.ValidationUtil;

/**
 * 通话记录观察者
 * 
 * @author 
 * 
 */
public class CallLogObserver extends ContentObserver {
	public CallLogObserver(Context context) {
		super(handler);
	}

	public void onChange(boolean paramBoolean) {
		super.onChange(paramBoolean);

		if (!ValidationUtil.isNullOrEmpty(MainApp.blockedNumber)) {
			try {
				new CallListBusiness().handleBlockedCall();
				handler.sendMessage(handler.obtainMessage());
			} catch (Exception e) {
				ExceptionHandler.handle(e, "通话记录观察者处理来电拦截时出错");
			}
		}
	}

	private static Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			for (AfterBlockedCallbackEvent callback : MainApp.getCallCallbackEvents()) {
				callback.invoke(msg);
			}
		};
	};
}
